package com.example.play.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.TextView
import com.example.common.entity.CommendEntity
import com.example.common.entity.CommendReply

import com.example.image.glide.GlideImgLoader
import com.example.play.R


class VideoCommentListAdapter constructor(private val list: MutableList<CommendEntity>):  BaseExpandableListAdapter(){


    override fun getGroupCount(): Int = list.size

    override fun getChildrenCount(groupPosition: Int): Int = list[groupPosition].replyList.size

    override fun getGroup(groupPosition: Int): CommendEntity = list[groupPosition]

    override fun getChild(groupPosition: Int, childPosition: Int): CommendReply = list[groupPosition].replyList[childPosition]

    override fun getGroupId(groupPosition: Int): Long = list[groupPosition].id.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = list[groupPosition].replyList[childPosition].id.toLong()

    override fun hasStableIds(): Boolean = false

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = false


    /**
     * 一级评论UI
     */
    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        var groupEntity = list[groupPosition]

        //方案一：使用DataBinding
//        val groupDataBinding = ItemCommentLeverOneBinding.inflate(LayoutInflater.from(parent?.context), parent, false)
//        GlideImgLoader.loadCircleImage(groupDataBinding.root.context, groupEntity.userlogo, groupDataBinding.civUserIcon)
//
//        //设置文本信息
//        groupDataBinding.apply {
//            tvNickName.text = if(groupEntity.nickname.isNullOrEmpty()) "小财神" else groupEntity.nickname
//            tvContent.text = groupEntity.content
//            tvCreateTime.text = "发布时间：${groupEntity.createtime}"
//            tvExpand.text = if (isExpanded) "——收起回复——" else "———展开回复——"
//        }
//
//        return groupDataBinding.root

        //复用之前的View
        var groupView = convertView ?: LayoutInflater.from(parent?.context).inflate(R.layout.item_comment_group, parent,false).apply {
            tag = GroupViewHolder(this)
        }

        var groupViewHolder = groupView.tag as GroupViewHolder
        GlideImgLoader.loadCircleImage(groupView.context, groupEntity.userlogo, groupViewHolder.ivUserLogo)
        groupViewHolder.apply {
            tvNickName.text = if(groupEntity.nickname.isNullOrEmpty()) "小财神" else groupEntity.nickname
            tvContent.text = groupEntity.content
            tvCreateTime.text = "发布时间：${groupEntity.createtime}"
            tvExpand.text = if (isExpanded) "——收起回复——" else "———展开回复——"
        }
        return groupView

    }

    /**
     * 二级评论UI
     */
    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        val childEntity = list[groupPosition].replyList[childPosition]
//        val childDataBinding = ItemCommendLeverTwoBinding.inflate(LayoutInflater.from(parent?.context), parent, false)
//        GlideImgLoader.loadCircleImage(childDataBinding.root.context, childEntity.userlogo,  childDataBinding.civUserIcon)
//        childDataBinding.tvNickName.text = if(childEntity.nickname.isNullOrEmpty()) "爱哭的小女孩" else childEntity.nickname
//        childDataBinding.tvContent.text = childEntity.content
//        childDataBinding.tvReplyTime.text = "回复时间:${childEntity.createtime}"
//        return childDataBinding.root


        //复用之前的View
        var childView = convertView ?: LayoutInflater.from(parent?.context).inflate(R.layout.item_commend_child, parent,false).apply {
            tag = ChildViewHolder(this)
        }

        var childViewHolder = childView.tag as ChildViewHolder
        GlideImgLoader.loadCircleImage(childView.context, childEntity.userlogo, childViewHolder.ivUserLogo)
        childViewHolder.apply {
            tvNickName.text = if(childEntity.nickname.isNullOrEmpty()) "爱哭的小女孩" else childEntity.nickname
            tvContent.text = childEntity.content
            tvReplyTime.text = "回复时间:${childEntity.createtime}"
        }
        return childView

    }

    inner class GroupViewHolder(groupView: View){
        var ivUserLogo: ImageView = groupView.findViewById(R.id.civ_user_icon)
        var tvNickName: TextView = groupView.findViewById(R.id.tv_nick_name)
        var tvContent: TextView = groupView.findViewById(R.id.tv_content)
        var tvCreateTime: TextView = groupView.findViewById(R.id.tv_create_time)
        var tvExpand: TextView = groupView.findViewById(R.id.tv_expand)
    }

    inner class ChildViewHolder(childView: View){
        var ivUserLogo: ImageView = childView.findViewById(R.id.civ_user_icon)
        var tvNickName: TextView = childView.findViewById(R.id.tv_nick_name)
        var tvContent: TextView = childView.findViewById(R.id.tv_content)
        var tvReplyTime: TextView = childView.findViewById(R.id.tv_reply_time)
    }



}